package netmon.snmp;

import com.adventnet.snmp.beans.SnmpTarget;

/**
 * Facade class which retreives data from via the AdventNet SNMP API
 * @author Marc Backes
 *
 */
public class SNMPEngine {
	private SnmpTarget target;		// Reference to 'SnmpTarget'
	private OIDConverter oc;		// Reference to 'OIDConverter'
	
	/**
	 * Initializes the SNMPEngine<br>
	 * Create converter, setup SnmpTargeet
	 */
	private void init(){
		oc = new OIDConverter();
		target = new SnmpTarget();
		target.setLoadFromCompiledMibs(true);
		target.setSnmpVersion(SnmpTarget.VERSION2C);
	}
	
	/**
	 * Set the hostname for the SNMP API
	 * @param host Hostname of the server
	 */
	public void setHost(String host){
		target.setTargetHost(host);
	}
	
	/**
	 * Contructor: Initializes the SNMPEngine
	 * 'public' will be used as community name
	 */
	public SNMPEngine(){
		init();
		target.setCommunity("public");
	}
	
	/**
	 * Contructor: Initializes the SNMPEngine
	 * 'community' will be used as community name
	 */
	public SNMPEngine(String community){
		init();
		target.setCommunity(community);
	}
	
	/**
	 * Returns the Ordered value with a given name as int
	 * @param OID Name of the object (name)
	 * @return int Value of the object
	 */
	public int getIntByName(String OID){
		Integer result;
		target.setObjectID(oc.get(OID));
		result = new Integer(target.snmpGet());
		if(result==null){
			System.err.println("SNMP request failed!");
			System.exit(1);
		}
		return result;
	}
	
	/**
	 * Returns the Ordered value with a given name as int
	 * @param OID Name of the object (name)
	 * @return int Value of the object
	 */
	public long getLongByName(String OID){
		Long result;
		target.setObjectID(oc.get(OID));
		result = new Long(target.snmpGet());
		if(result==null){
			System.err.println("SNMP request failed!");
			System.exit(1);
		}
		return result;
	}
	
	/**
	 * Returns the Ordered value with a given name as double
	 * @param OID Name of the object (name)
	 * @return double Value of the object
	 */
	public double getDoubleByName(String OID){
		Double result;
		target.setObjectID(oc.get(OID));
		result = new Double(target.snmpGet());
		if(result==null){
			System.err.println("SNMP request failed!");
			System.exit(1);
		}
		return result;
	}
	
	/**
	 * Returns the Ordered value with a given name as String
	 * @param OID Name of the object (name)
	 * @return String Value of the object
	 */
	public String getStringByName(String OID){
		String result;
		target.setObjectID(oc.get(OID));
		result = "";
		try {
			 result = new String(target.snmpGet());
		    } catch (Exception ex) {
		        System.err.println("SNMP request failed!");
		        System.exit(1);
		    }/*
		if(result==null){
			System.err.println("SNMP request failed!");
			System.exit(1);
		}*/
		return result;
	}
	
	/**
	 * Returns the Ordered value with a given OID as int
	 * @param OID ID of the object (Object ID)
	 * @return int Value of the object
	 */
	public int getIntById(String OID){
		Integer result;
		target.setObjectID(OID);
		result = new Integer(target.snmpGet());
		if(result==null){
			System.err.println("SNMP request failed!");
			System.exit(1);
		}
		return result;
	}
	
	/**
	 * Returns the Ordered value with a given OID as double
	 * @param OID ID of the object (Object ID)
	 * @return double Value of the object
	 */
	public double getDoubleById(String OID){
		Double result;
		target.setObjectID(OID);
		result = new Double(target.snmpGet());
		if(result==null){
			System.err.println("SNMP request failed!");
			System.exit(1);
		}
		return result;
	}
	
	/**
	 * Returns the Ordered value with a given OID as String
	 * @param OID ID of the object (Object ID)
	 * @return String Value of the object
	 */
	public String getStringById(String OID){
		String result;
		target.setObjectID(OID);
		result = target.snmpGet();
		if(result==null){
			System.err.println("SNMP request failed!");
			System.exit(1);
		}
		return result;
	}
	
	/**
	 * Returns the Ordered value with a given OID as String
	 * @param OID ID of the object (Object ID)
	 * @return String Value of the object
	 */
	public long getLongById(String OID){
		Long result;
		target.setObjectID(OID);
		result = new Long(target.snmpGet());
		if(result==null){
			System.err.println("SNMP request failed!");
			System.exit(1);
		}
		return result;
	}
}
